// backend/src/config/db.js
import { Sequelize } from "sequelize";
import fs from "fs";
import yaml from "js-yaml";

// 读取 YAML 配置文件
const config = yaml.load(fs.readFileSync('./src/application.yaml', 'utf8'));

// 从配置中提取数据库信息
const dbConfig = config.database;

const sequelize = new Sequelize(
    dbConfig.name,
    dbConfig.user,
    dbConfig.password,
    {
        host: dbConfig.host,
        port: dbConfig.port,
        dialect: 'mysql',
    }
);

// 连接验证
sequelize.authenticate()
    .then(() => console.log('Connected to MySQL'))
    .catch(err => console.error('MySQL 连接错误:', err));

// 导出实例
export default sequelize; // 使用 export default 导出 sequelize 实例
